package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.BaseException;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;

    @Override
    public PageResult setmealPageQuery(SetmealPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<SetmealVO> page = setmealMapper.setmealPageQuery(dto);
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        return new PageResult<>(total, result);
    }

    @Override
    @Transactional
    public void saveSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto, setmeal);
        setmeal.setStatus(StatusConstant.DISABLE);
        setmealMapper.saveSetmeal(setmeal);
        Long id = setmeal.getId();

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(id);
        }
        setMealDishMapper.saveSetmealDish(setmealDishes);


    }

    @Override
    @Transactional
    public void deleteSetmeal(List<Long> ids) {

       List<SetmealVO> setmealVOList =  setmealMapper.getByIds(ids);
        for (SetmealVO setmealVO : setmealVOList) {
            Integer status = setmealVO.getStatus();
            if(status.equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        setmealMapper.deleteSetmeal(ids);

        setMealDishMapper.deleteSetmealDish(ids);
    }

    @Override
    public void status(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.updataSetmeal(setmeal);
    }

    @Override
    public SetmealVO getByid(Long id) {
        SetmealVO setmealVO = setmealMapper.getByid(id);

        SetmealDish build = SetmealDish.builder().setmealId(id).build();
        List<SetmealDish> setmealDish = setMealDishMapper.getByDish(build);

//        List<SetmealDish> setmealDish = setMealDishMapper.getByDishId(Collections.singletonList(id));

    setmealVO.setSetmealDishes(setmealDish);

        return setmealVO;
    }

    @Override
    @Transactional
    public void updataSetmeal(SetmealDTO dto) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(dto,setmeal);
        setmealMapper.updataSetmeal(setmeal);
        Long id = dto.getId();

        List<SetmealDish> setmealDishes = dto.getSetmealDishes();
        setMealDishMapper.deleteSetmealDish(Collections.singletonList(id));
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setDishId(id);
        }
        if (setmealDishes != null && !setmealDishes.isEmpty()) {


            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(id);
            }
            setMealDishMapper.saveSetmealDish(setmealDishes);
        }
    }
}
